from django.shortcuts import render
from django.db.models import Q
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.decorators import api_view, action
from rest_framework import status
from rest_framework import generics
from rest_framework.viewsets import ModelViewSet
from rest_framework import permissions
from .models import Genome
from .serializers import GenomeSerializer
from .permissions import IsOwnerOrReadOnly

# Create your views here.

# Function API View


# @api_view(['GET', 'POST'])
# def genome_list_or_create(request):
#     """
#     List genomes or Create new genome
#     """
#     if request.method == "GET":
#         genomes = Genome.objects.all()
#         genomes_serializer = GenomeSerializer(genomes, many=True)
#         return Response(genomes_serializer.data, status=status.HTTP_200_OK)
#     elif request.method == "POST":
#         genome_serializer = GenomeSerializer(data=request.data)
#         if genome_serializer.is_valid():
#             genome_serializer.save()
#             return Response(genome_serializer.data, status=status.HTTP_201_CREATED)
#         else:
#             return Response(genome_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# class GenomeViewList(APIView):

#     def get(self, request, format=None):
#         genomes = Genome.objects.all()
#         genomes_serializer = GenomeSerializer(genomes, many=True)
#         return Response(genomes_serializer.data, status=status.HTTP_200_OK)

#     def post(self, request, format=None):
#         genome_serializer = GenomeSerializer(data=request.data)
#         if genome_serializer.is_valid():
#             genome_serializer.save()
#             return Response(genome_serializer.data, status=status.HTTP_201_CREATED)
#         else:
#             return Response(genome_serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# class GenomeViewDetail(APIView):

#     def put(self, request, pk, format=None):
#         try:
#             genome = Genome.objects.get(pk=pk)
#         except Genome.DoesNotExist:
#             return Response(status=status.HTTP_404_NOT_FOUND)

#         genome_serializer = GenomeSerializer(genome, data=request.data)
#         if genome_serializer.is_valid():
#             genome_serializer.save()
#             return Response(genome_serializer.data, status=status.HTTP_200_OK)
#         else:
#             return Response(genome_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

# class GenomeViewList(generics.ListCreateAPIView):
#     queryset = Genome.objects.all()
#     serializer_class = GenomeSerializer

#     def perform_create(self, serializer):
#         # do something..
#         instance = serializer.save()
#         # do something..


# class GenomeViewDetail(generics.RetrieveUpdateDestroyAPIView):
#     queryset = Genome.objects.all()
#     serializer_class = GenomeSerializer

class GenomeViewSet(ModelViewSet):
    queryset = Genome.objects.all()
    serializer_class = GenomeSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly,
                          IsOwnerOrReadOnly,
                          ]

    @action(detail=True)
    def abstract(self, request, pk=None):
        genome = self.get_object()
        abstract = genome.show_abstract()
        return Response({"abstract": abstract}, status=status.HTTP_200_OK)

    @action(detail=False, methods=['get'])
    def query(self, request):
        queryset = self.get_queryset()
        query = request.query_params.get("query")
        if query:
            queryset = queryset.filter(
                Q(en_name__icontains=query) | Q(cn_name__icontains=query))
        serializer = self.get_serializer_class()
        gs = serializer(queryset, many=True)
        return Response(gs.data)

    def perform_create(self, serializer):
        serializer.save(submiter=self.request.user)
